package com.heita.server;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;

public class TableTrans {

	public static HashMap<String, String> getNameMap() {
		HashMap<String, String> nameMap = new HashMap<String, String>();
		nameMap.put("Misaka Mikoto", "御坂美琴");
		nameMap.put("Tachibana Kanade", "立华奏");
		nameMap.put("Nakano Azusa", "中野梓");
		nameMap.put("Nagato Yuki", "长门有希");
		nameMap.put("Shana", "夏娜");
		nameMap.put("Gokō Ruri (Kuroneko)", "黑猫");
		nameMap.put("Eucliwood Hellscythe", "优库里伍德");
		nameMap.put("Akiyama Mio", "秋山澪");
		nameMap.put("Aisaka Taiga", "逢坂大河");
		nameMap.put("Katsura Hinagiku", "桂雏菊");
		nameMap.put("Yui", "由衣");
		nameMap.put("Charlotte Dunois", "夏洛特");
		nameMap.put("Nakamura Yuri", "仲村由理");
		nameMap.put("Suzumiya Haruhi", "凉宫春日");
		nameMap.put("Saber", "Saber");
		nameMap.put("Sengoku Nadeko", "千石抚子");
		nameMap.put("Senjōgahara Hitagi", "战场原黑仪");
		nameMap.put("Victorique de Blois", "维克多利加");
		nameMap.put("Sakagami Tomoyo", "坂上智代");
		nameMap.put("Kōsaka Kirino", "高坂桐乃");
		nameMap.put("Hirasawa Yui", "平泽唯");
		nameMap.put("Sanzen'in Nagi", "三千院凪");
		nameMap.put("Fujibayashi Kyō", "藤林杏");
		nameMap.put("Hiiragi Kagami", "柊镜");
		nameMap.put("Konjiki no Yami", "金色之暗");
		nameMap.put("Izumi Konata", "泉此方");
		nameMap.put("Furukawa Nagisa", "古河渚");
		nameMap.put("Aragaki Ayase", "新垣绫濑");
		nameMap.put("Asahina Mikuru", "朝比奈实玖瑠");
		nameMap.put("Kuroi Mato", "黑岩");
		nameMap.put("Holo", "赫萝");
		nameMap.put("C.C.", "C.C.");
		nameMap.put("Index L. Prohibitorum", "目录");
		nameMap.put("Louise Vallière", "路易兹");
		nameMap.put("Kotobuki Tsumugi", "琴吹紬");
		nameMap.put("Fate Testarossa", "菲特");
		nameMap.put("Shiina Mafuyu", "椎名真冬");
		nameMap.put("Hirasawa Ui", "平泽忧");
		nameMap.put("Last Order", "最后之作");
		nameMap.put("Shirai Kuroko", "白井黑子");
		nameMap.put("Haruna", "春奈");
		nameMap.put("Tōsaka Rin", "远坂凛");
		nameMap.put("Iwasawa Asami", "岩泽麻美");
		nameMap.put("Nymph", "尼姆芙");
		nameMap.put("Shinonono Hōki", "筱之之箒");
		nameMap.put("Seraphim", "瑟拉芬");
		nameMap.put("Huang Lingyin", "黄玲音");
		nameMap.put("Kasugano Sora", "春日野穹");
		nameMap.put("Shiina", "椎名");
		nameMap.put("Laura Bodewig", "萝拉");

		return nameMap;
	}

	public static void transISML(String fileName) {

		String split = "\t";

		String result = "<table>";
		result += "<tr>";
		result += "<td>";
		result += "排名";
		result += "</td>";
		result += "<td>";
		result += "姓名";
		result += "</td>";
		result += "<td>";
		result += "积分";
		result += "</td>";
		result += "<td>";
		result += "赢";
		result += "</td>";
		result += "<td>";
		result += "平";
		result += "</td>";
		result += "<td>";
		result += "输";
		result += "</td>";
		result += "<td>";
		result += "SoS";
		result += "</td>";
		result += "<td>";
		result += "SDO";
		result += "</td>";
		result += "<td>";
		result += "SAO";
		result += "</td>";
		result += "<td>";
		result += "VD";
		result += "</td>";
		result += "<td>";
		result += "VF";
		result += "</td>";
		result += "<td>";
		result += "VA";
		result += "</td>";
		result += "<td>";
		result += "pt%";
		result += "</td>";
		result += "<td>";
		result += "PB";
		result += "</td>";
		result += "<td>";
		result += "</tr>";

		BufferedReader reader = null;
		HashMap<String, String> nameMap = getNameMap();
		String name;
		try {
			FileInputStream fis = new FileInputStream(fileName);
			InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
			reader = new BufferedReader(isr);
			String tempString = null;
			int line = 1;

			while ((tempString = reader.readLine()) != null) {
				// System.out.println("temp string=" + tempString);

				String[] array = tempString.split("\t");
				result += "<tr>";
				result += "<td>";
				result += array[0];
				result += "</td>";

				name = nameMap.get(array[1]);

				// while (name.length() < 10) {
				// name += " ";
				// }
				result += "<td>";
				result += name;
				result += "</td>";
				for (int i = 2; i < 14; i++) {
					result += "<td>";
					result += array[i];
					result += "</td>";
				}
				result += "</tr>";
				// if (name != null) {
				// tempString = tempString.replace(array[1], name);
				// }
				// result+=tempString+"\n";
				line++;
			}
			System.out.println(result);

			reader.close();
			TableTrans.outputFile("F://test.html", result);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void outputFile(String fileName, String content) {
		try {
			FileOutputStream fos;
			fos = new FileOutputStream(fileName);
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(content);
			osw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
